import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import store from "@/stores";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home.vue"),
    meta: {
      title: "首页 | Share Plant",
    },
    children: [
      {
        path: "welcome",
        component: () => import("../views/navigation/Welcome.vue"),
      },
      {
        path: "category",
        name: "Category",
        component: () => import("../views/navigation/Category.vue"),
        meta: {
          title: "种群 | Share Plant",
        },
      },
      {
        path: "search/:search",
        name: "Search",
        component: () => import("../views/navigation/Search.vue"),
        meta: {
          title: "图片检索 | Share Plant",
        },
      },
      {
        path: "detail/:id",
        name: 'detail',
        component: () => import("../views/plant/PlantDetail.vue"),
      },
      {
        path: "subject",
        component: () => import("../views/navigation/Subject.vue"),
      },
      {
        path: "discovery",
        component: () => import("../views/navigation/Discovery.vue"),
        meta: {
          title: "发现 | Share Plant",
        },
      },
      {
        path: "college",
        component: () => import("../views/navigation/College.vue"),
        meta: {
          title: "校园 | Share Plant",
        },
      },
      {
        path: "about",
        component: () => import("../views/navigation/About.vue"),
        meta: {
          title: "联系我们 | Share Plant",
        },
      },
      {
        path: "appraise",
        component: () => import("../views/navigation/Appraise.vue"),
        meta: {
          title: "鉴定 | Share Plant",
        },
      },
      {
        path: "choiceness",
        component: () => import("../views/navigation/Choiceness.vue"),
        meta: {
          title: "精选 | Share Plant",
        },
      },
      {
        path: "state",
        component: () => import("../views/navigation/State.vue"),
        meta: {
          title: "动态 | Share Plant",
        },
      },
      {
        path: "user",
        children: [
          {
            path: "appraise",
            component: () => import("../views/user/Appraise.vue"),
            meta: {
              title: "我的鉴定 | Share Plant",
            },
          },
          {
            path: "collect",
            component: () => import("../views/user/Collect.vue"),
            meta: {
              title: "我的收藏 | Share Plant",
            },
          },
          {
            path: "comment",
            component: () => import("../views/user/Comment.vue"),
            meta: {
              title: "我的评论 | Share Plant",
            },
          },
          {
            path: "fans",
            component: () => import("../views/user/Fans.vue"),
            meta: {
              title: "我的粉丝 | Share Plant",
            },
          },
          {
            path: "follow",
            component: () => import("../views/user/Follow.vue"),
            meta: {
              title: "我的关注 | Share Plant",
            },
          },
          {
            path: "setting",
            component: () => import("../views/user/Setting.vue"),
            meta: {
              title: "账号设置 | Share Plant",
            },
            children: [
              {
                path: "",
                component: () => import("../views/user/user-setting/Detail.vue"),
                meta: {
                  title: "用户信息 | Share Plant",
                },
              },
              {
                path: "detail",
                name: "UserDetail",
                component: () => import("../views/user/user-setting/Detail.vue"),
                meta: {
                  title: "用户信息 | Share Plant",
                },
              },
              {
                path: "update",
                name: "UpdatePassword",
                component: () => import("../views/user/user-setting/UpdatePassword.vue"),
                meta: {
                  title: "修改密码 | Share Plant",
                },
              },
              {
                path: "campus",
                name: "Campus",
                component: () => import("../views/user/user-setting/Campus.vue"),
                meta: {
                  title: "校园认证 | Share Plant",
                },
              },
              {
                path: "email",
                name: "BindEmail",
                component: () => import("../views/user/user-setting/BindEmail.vue"),
                meta: {
                  title: "绑定邮箱 | Share Plant",
                },
              },
            ]
          },
          {
            path: "subject",
            component: () => import("../views/user/Subject.vue"),
            meta: {
              title: "我的专题 | Share Plant",
            },
          },
          {
            path: "works",
            component: () => import("../views/user/Works.vue"),
            meta: {
              title: "我的作品 | Share Plant",
            },
          },
        ]
      },
    ]
  },



  // {
  //   path: "/login",
  //   component: () => import("../views/login/Login.vue"),
  //   meta: {
  //     title: "用户登录 | Share Plant",
  //   },
  // },
  // {
  //   path: "/register",
  //   component: () => import("../views/login/Register.vue"),
  //   meta: {
  //     title: "用户注册 | Share Plant",
  //   },
  // },
  {
    path: "/404",
    name: '404',
    component: () => import("../views/404.vue"),
    meta: {
      title: "找不到该页面 | Share Plant",
    },
  },
  {
    path: '/redirect/:path*',
    component: () => import("../views/redirect/Index.vue"),
    meta: {
      title: "Share Plant",
    },
  }
];

export const errorRoute: RouteRecordRaw = {
  path: '/:pathMatch(.*)',
  redirect: '/404'
}

const router = createRouter({
  history: createWebHashHistory(), // 路由格式
  routes: routes, // 路由数组
});

// 路由跳转前，获取用户信息
router.beforeEach(async (to, from, next) => {
  // token存在的情况
  if (store.useUserStore.token) {
    if (!store.useUserStore.user.userId) {
      await store.useUserStore.getUserInfoAction()
      if (to.path === '/') {
        sessionStorage.setItem("navigation_index", '0')
        next('/welcome')
      } 
      next()
    } else {
      if (to.path === '/') {
        sessionStorage.setItem("navigation_index", '0')
        next('/welcome')
      } 
      next()
    }

  } else {
    if (to.path === '/' || to.path === '/user') {
      sessionStorage.setItem("navigation_index", '0')
      next('/welcome')
    } else {
      next()
    }
  }
})

// 路由跳转后，设置网页标题
router.afterEach((to, from) => {
  document.title = to.meta.title?.toString() || "";
});

export default router;
